package com.sqx.modules.postPush.controller;

import com.sqx.common.utils.Result;
import com.sqx.modules.postPush.entity.PostPush;
import com.sqx.modules.postPush.service.PostPushService;
import com.sqx.modules.postPush.vo.PostPushVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@RestController
@Api(value = "招聘信息", tags = {"招聘信息"})
@RequestMapping(value = "/admin/postPush")
public class PostPushController {

    @Autowired
    private PostPushService postPushService;

    @PostMapping("/savePostPush")
    @ApiOperation("保存招聘信息")
    public Result savePostPush(PostPush postPush) {
        return postPushService.savePostPush(postPush);
    }

    @GetMapping("/getPostPushList")
    @ApiOperation("获取所有的招聘|求职岗位列表")
    public Result getPostPushList(Integer page, Integer limit, PostPushVo postPushVo, String startTime, String endTime) {
        return Result.success().put("data", postPushService.getPostPushList(page, limit, postPushVo, startTime, endTime));
    }

    @PostMapping("/deletePostPush")
    @ApiOperation("删除招聘信息")
    public Result deletePostPush(Long userId, Long postPushId) {
        return postPushService.deletePostPush(userId, postPushId);
    }

    @PostMapping("/auditPostPush")
    @ApiOperation("审核招聘信息")
    public Result auditPostPush(Long postPushId, String content, Integer status) {
        return postPushService.auditPostPush(postPushId, content, status);
    }

    @PostMapping("/updatePostPushIsRecommend")
    @ApiOperation("修改是否推荐")
    public Result updatePostPushIsRecommend(Long postPushId) {
        PostPush postPush = postPushService.getById(postPushId);
        if (postPush.getIsRecommend() == null || postPush.getIsRecommend() == 1) {
            postPush.setIsRecommend(0);
        } else {
            postPush.setIsRecommend(1);
        }
        postPushService.updateById(postPush);
        return Result.success();
    }


    @PostMapping("/banPostPush")
    @ApiOperation("封禁招聘|求职信息")
    public Result banPostPush(Long postPushId, String examineContent) {
        return postPushService.banPostPush(postPushId, examineContent);
    }

    @PostMapping("/updatePostPushIsTop")
    @ApiOperation("修改是否置顶")
    public Result updatePostPushIsTop(Long postPushId, String topEndTime) {
        PostPush postPush = postPushService.getById(postPushId);
        if (postPush.getIsTop() == null || postPush.getIsTop() == 1) {
            postPush.setIsTop(0);
            postPush.setTopEndTime(null);
        } else {
            postPush.setIsTop(1);
            if (StringUtils.isNotBlank(topEndTime)) {
                postPush.setTopEndTime(LocalDateTime.parse(topEndTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            }

        }
        postPushService.updateById(postPush);
        return Result.success();
    }

    @GetMapping("/selectPostPushDetails")
    @ApiOperation("查询招聘信息详情")
    public Result selectPostPushDetails(Long postPushId) {
        return Result.success().put("data", postPushService.selectPostPushDetails(postPushId, null));
    }

    @GetMapping("/getPoshPushPhone")
    @ApiOperation("根据招聘或简历查看手机号")
    public Result getPoshPushPhone(Long postPushId) {
        return Result.success().put("data", postPushService.getPoshPushPhone(postPushId));
    }
    @ApiOperation("修改上下架状态")
    @GetMapping("updateStatus")
    public Result updateStatus(Long postPushId) {
        return postPushService.updateStatus(postPushId);
    }
}
